﻿using System.Collections.Generic;
using DesignerTables;
using UnityEngine;

namespace DesignerScripts
{
    /// <summary>
    /// 行动类型枚举
    /// </summary>
    public enum ActionType
    {
        select,
        moveToPos,
        attack,
    }
    
    public class PawnActionScripts
    {
        public Dictionary<ActionType,ActionDelegation> Func;

        public PawnActionScripts()
        {
            Func = new Dictionary<ActionType, ActionDelegation>(){
                {ActionType.select, Selected},
                {ActionType.attack, Attack},
                {ActionType.moveToPos, MoveToPos}
            }; 
        }
        
        ///<summary>
        /// 选中了某个棋子
        ///</summary>
        private void Selected(ActionInfo actionInfo){
            //如果选中了某个棋子
            // Debug.Log($"{cursorPos}");
            var levelInfoObj = actionInfo.LevelInfoObj;
            var moveTarget = levelInfoObj.GetPawnByPos(actionInfo.TargetPos);
            if (moveTarget == null) return;
            var pawnState = moveTarget.GetComponent<PawnState>();
            if (!pawnState.ControlState.CanAction())
            {
                Debug.Log("这个棋子已经行动过了");
            }
            //暂时只获得了移动的范围，可以再获得攻击的范围，和移动范围做差来获得最远攻击范围
            var PawnMoveList = levelInfoObj.GetPawnMoveList(actionInfo.TargetPos,moveTarget.GetComponent<PawnState>());
            var PawnActionMoveList = levelInfoObj.GetPawnMoveAttackList(PawnMoveList, moveTarget.GetComponent<PawnState>());
            foreach (var pos in PawnMoveList)
            {
                GameState.ShowMoveMapDict.Add(LevelInfoObj.GetMapByPos(pos).transform,0);
            }

            foreach (var pos in PawnActionMoveList)
            {
                if (!GameState.ShowMoveMapDict.ContainsKey(LevelInfoObj.GetMapByPos(pos).transform))
                {
                    GameState.AttackRangeDict.Add(LevelInfoObj.GetMapByPos(pos).transform,0); 
                }
            }
            pawnState.ControlState.CoverActionList(new [] { ActionType.moveToPos});
            pawnState.ControlState.DefaultAction = ActionType.moveToPos;
            SceneVariants.GetLevelManager().SelectPawn = moveTarget;
        }
        
        /// <summary>
        /// 攻击的提交处理
        /// </summary>
        private void Attack(ActionInfo actionInfo)
        {
            //如果选中了某个目标 这个目标不能是自己
            var levelInfoObj = actionInfo.LevelInfoObj;
            var target = levelInfoObj.GetPawnByPos(actionInfo.TargetPos);
            if (target != null && !target.Equals(actionInfo.Pawn))
            {
                SceneVariants.CreateDamage(
                    actionInfo.Pawn, 
                    target,
                    new Damage(actionInfo.Pawn.GetComponent<PawnState>().GetAttack()), 
                    0,
                    new DamageInfoTag[]{DamageInfoTag.directDamage,}
                );
                GameState.AttackObjectInfos.Clear();
                GameState.AttackRangeDict.Clear();
                actionInfo.Pawn.GetComponent<PawnState>().ControlState.RemoveAction(ActionType.attack);
                SceneVariants.GetLevelManager().PawnActionOver();
            }
        }
        
        /// <summary>
        /// 移动到某个位置处理
        /// </summary>
        private void MoveToPos(ActionInfo actionInfo)
        {
            //如果选中了某个位置
            var levelInfoObj = actionInfo.LevelInfoObj;
            var moveTarget = levelInfoObj.GetPawnByPos(actionInfo.TargetPos);
            if (moveTarget == null || moveTarget.Equals(actionInfo.Pawn))
            {
                actionInfo.Pawn.GetComponent<PawnState>().ControlState.RemoveAction(ActionType.moveToPos);
                var pawnState = actionInfo.Pawn.GetComponent<PawnState>();
                //如果当前操作阵营和棋子阵营不同
                if (actionInfo.LevelInfoObj.CurActionCamp != pawnState.CampId)
                {
                    return;
                }
    
                if (GameState.ShowMoveMapDict.ContainsKey(LevelInfoObj.GetMapByPos(actionInfo.TargetPos).transform) 
                    && levelInfoObj.MovePawn(actionInfo.TargetPos, actionInfo.Pawn))
                {
                    GameState.ShowMoveMapDict.Clear();
                    GameState.AttackRangeDict.Clear();
                    //显示攻击范围
                    var temp = levelInfoObj.GetPawnAttackList(actionInfo.TargetPos, actionInfo.Pawn.GetComponent<PawnState>());
                    foreach (var v in temp)
                    {
                        var canAttackPawn = levelInfoObj.GetPawnByPos(v);
                        if (canAttackPawn != null)
                        {
                            // 只有准攻击的才进入攻击列表
                            if (LevelInfoObj.CheckCanOperate(CampPowerType.Attack,pawnState,canAttackPawn.GetComponent<PawnState>()))
                            {
                                GameState.AttackObjectInfos.Add(new AttackObjectInfo(canAttackPawn, v));
                            }
                        }
                        GameState.AttackRangeDict.Add(LevelInfoObj.GetMapByPos(v).transform, 0);
                    }
    
                    if (GameState.AttackObjectInfos.Count != 0)
                    {
                        GameState.FocusPos = GameState.AttackObjectInfos[0].Pos;
                        GameState.AttackObjectIndex = 0;
                        pawnState.ControlState.DefaultAction = ActionType.attack;
                        pawnState.ControlState.CoverActionList(new [] { ActionType.attack});
                    }
                    else
                    {
                        GameState.AttackRangeDict.Clear();
                        SceneVariants.GetLevelManager().PawnActionOver();
                    }
                }
            }
        }
    }
}